package com.smile.ye.handler;


import com.smile.ye.response.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletResponse;
@RestControllerAdvice
@Slf4j
public class  GlobalExceptionHandler {
    @Autowired
    HttpServletResponse httpServletResponse;
    @ExceptionHandler(RuntimeException.class)
    public Result handler(RuntimeException e){
        System.out.println(e.getMessage());
            String message=e.getMessage();
        System.out.println(httpServletResponse.getStatus());
        httpServletResponse.setStatus(500);
        if(e instanceof BadCredentialsException) message = "账号或密码错误!";
        log.error( message+ "globalException");
        return Result.fail(message);
    }
}
